<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/7 14:35
 * Remark:
 */

namespace App\Func;


use App\Models\SystemLog\SystemRequestLog;

class CurlFunc
{

    public static function CurlGet($url,$type = 1,$data=[],$header=[]){
        // 请求日志
        $curl = curl_init();
        if($data){
            $prams ="";
            foreach($data as $key=>$value){
                $prams .="&$key=$value";
            }
            $prams = substr_replace($prams,"?",stripos($prams,"&"),1);
            $url = $url.$prams;
        }
        curl_setopt($curl,CURLOPT_TIMEOUT,5000);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($curl,CURLOPT_USERAGENT,false);
        curl_setopt($curl,CURLOPT_URL,$url);
        if ($header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        if($type == 1){
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        }
        $res = curl_exec($curl);
   //     echo "curl_".$res;
//        $sqm = new SystemRequestLog();
//        $sqm->add([
//            'request_info' => json_encode($data,256),
//            'response_info' => $res,
//            'request_url' => $url,
//            'request_header' => json_encode($header,256),
//            'owner_id' => TokenFuc::getCurrentStaffInfo()['staff_id'] ?? 0
//        ]);
        if($res){
            curl_close($curl);
            return $res;
        }else {
            $error = curl_errno($curl);
            curl_close($curl);
            return $error;
        }
    }

    public static function CurlPost($url , $data=array(),$header = []): bool|string
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,256));
        $output = curl_exec($ch);
        curl_close($ch);
//        $sqm = new SystemRequestLog();
//        $sqm->add([
//            'request_info' => json_encode($data,256),
//            'response_info' => $output,
//            'request_url' => $url,
//            'request_header' => json_encode($header,256),
//            'owner_id' => TokenFuc::getCurrentStaffInfo()['staff_id'] ?? 0
//        ]);
        return $output;
    }

    public static function sendPost($url,$post_data): bool|string
    {
        $postdata = http_build_query($post_data);
        $options = array (
            'http' => array (
                'method' => 'POST' ,
                'header' => 'Content-type:application/x-www-form-urlencoded' ,
                'content' => $postdata ,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create( $options );
        return file_get_contents ( $url , false, $context );
    }

}